<?php

$sent = '-123x^4+567x+89';
if (! empty($_REQUEST['sent'])) {
    $sent = urldecode($_REQUEST['sent']);
}
header('Content-Type: text/plain; charset=UTF-8');
$php = file_get_contents('http://local.com/parser/generate.php?text&file=grammar');
//eval($php);
//exit($php);
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'sets'.DIRECTORY_SEPARATOR.'set.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'parser.php';

class Polynom extends Parser
{

    function __construct()
    {
        parent::__construct();
        $this->main = "Polynom";
    }

    function NT_Polynom()
    {
        $this->createChild('Polynom');

        if ($this->Ch() == null) {
            $this->Error();
        }
        if (($this->Ch() != null) && Set(new UnionSet(new SingleSet(str::getCode('+')), new SingleSet(str::getCode('-'))))->has_a(str::getCode($this->Ch()))) {
            $set = new UnionSet(new SingleSet(str::getCode('+')), new SingleSet(str::getCode('-')));
            if (! $set->has_a(str::getCode($this->Ch()))) {
                $this->Error();
            }
            $this->NextCh();
        }
        if ($this->Ch() == null) {
            $this->Error();
        }
        $this->NT_Addent();
        while (($this->Ch() != null) && Set(new UnionSet(new SingleSet(str::getCode('+')), new SingleSet(str::getCode('-'))))->has_a(str::getCode($this->Ch()))) {
            if ($this->Ch() == null) {
                $this->Error();
            }
            $set = new UnionSet(new SingleSet(str::getCode('+')), new SingleSet(str::getCode('-')));
            if (! $set->has_a(str::getCode($this->Ch()))) {
                $this->Error();
            }
            $this->NextCh();
            if ($this->Ch() == null) {
                $this->Error();
            }
            $this->NT_Addent();
        }

        $this->endChild();
    }

    function NT_Addent()
    {
        $this->createChild('Addent');

        if ($this->Ch() == null) {
            $this->Error();
        }
        if ($this->Ch() == null) {
            $this->Error();
        }
        switch (true) {

        case Set(new UnionSet(new RangeSet(str::getCode('0'), str::getCode('9'))))->has_a(str::getCode($this->Ch())):
            if ($this->Ch() == null) {
                $this->Error();
            }
            if ($this->Ch() == null) {
                $this->Error();
            }
            $this->NT_Number();
            if (($this->Ch() != null) && Set(new SingleSet(str::getCode('x')))->has_a(str::getCode($this->Ch()))) {
                if ($this->Ch() == null) {
                    $this->Error();
                }
                foreach (str('x') as $char) {
                    if ($this->Ch() != $char) {
                        $this->Error();
                    }
                    $this->NextCh();
                    if ($this->Ch() == null) {
                        $this->Error();
                    }
                }
                if (($this->Ch() != null) && Set(new SingleSet(str::getCode('^')))->has_a(str::getCode($this->Ch()))) {
                    $this->NT_Power();
                }
            }
            break;

        case Set(new SingleSet(str::getCode('x')))->has_a(str::getCode($this->Ch())):
            if ($this->Ch() == null) {
                $this->Error();
            }
            if ($this->Ch() == null) {
                $this->Error();
            }
            foreach (str('x') as $char) {
                if ($this->Ch() != $char) {
                    $this->Error();
                }
                $this->NextCh();
                if ($this->Ch() == null) {
                    $this->Error();
                }
            }
            if (($this->Ch() != null) && Set(new SingleSet(str::getCode('^')))->has_a(str::getCode($this->Ch()))) {
                $this->NT_Power();
            }
            break;

        default:
            $this->Error();
        }

        $this->endChild();
    }

    function NT_Power()
    {
        $this->createChild('Power');

        if ($this->Ch() == null) {
            $this->Error();
        }
        if ($this->Ch() == null) {
            $this->Error();
        }
        foreach (str('^') as $char) {
            if ($this->Ch() != $char) {
                $this->Error();
            }
            $this->NextCh();
            if ($this->Ch() == null) {
                $this->Error();
            }
        }
        if ($this->Ch() == null) {
            $this->Error();
        }
        $this->NT_Number();

        $this->endChild();
    }

    function NT_Number()
    {
        $this->createChild('Number');

        if ($this->Ch() == null) {
            $this->Error();
        }
        do {
            $set = new UnionSet(new RangeSet(str::getCode('0'), str::getCode('9')));
            if (! $set->has_a(str::getCode($this->Ch()))) {
                $this->Error();
            }
            $this->NextCh();
        } while (($this->Ch() != null) && Set(new UnionSet(new RangeSet(str::getCode('0'), str::getCode('9'))))->has_a(str::getCode($this->Ch())));

        $this->endChild();
    }

}


$parser = new Polynom();
header('Content-Type: text/xml; charset=UTF-8');
echo $parser->Process($sent)->saveXML();

?>